import { Model } from 'dva';
import { delay } from '@/public/utils';
const matchPathName = function (menus: any[], pathname: string): any {
  let defaultOpenKey, defaultSelectedKey;
  if (!menus || !menus.length) return {}
  for (const item of menus) {
    const { children, value, href } = item;
    if (href === pathname) {
      defaultSelectedKey = value;
      break;
    } else {
      defaultOpenKey = value;
      defaultSelectedKey = matchPathName(children, pathname).defaultSelectedKey;
      if (defaultSelectedKey) break;
    }
  }
  return {
    defaultOpenKey,
    defaultSelectedKey
  }
}
export default {
  namespace: 'global',
  state: {
    userInfo: {},
    menus: [],
    defaultOpenKeys: [],
    defaultSelectedKeys: []
  },
  effects: {
    *fetchMenus({ payload }, { put, call }) {
      yield call(delay, 100);
      const menus = [
        {
          icon: 'user',
          label: '用户管理',
          value: '1',
          children: [
            {
              label: '用户列表',
              value: '11',
              href: '/app/user/list',
            },
          ]
        },
        {
          icon: 'solution',
          label: '师资管理',
          value: '2',
          children: [
            {
              label: '专家列表',
              value: '21',
              href: '/app/teachers/expert/list',
            },
            {
              label: '名师列表',
              value: '22',
              href: '/app/teachers/master/list',
            },
          ]
        },
        {
          icon: 'file',
          label: '资料管理',
          value: '3',
          children: [
            {
              label: '文库列表',
              value: '31',
              href: '/app/database/document/list',
            },
            {
              label: '视频/音频列表',
              value: '32',
              href: '/app/database/media/list',
            },
          ]
        },
        {
          icon: 'exception',
          label: '园所管理',
          value: '4',
          children: [
            {
              label: '园所列表',
              value: '41',
              href: '/app/kindergarten/list',
            },
            {
              label: '审核',
              value: '42',
              href: '/app/kindergarten/audit',
            },
          ]
        },
        {
          icon: 'home',
          label: '首页管理',
          value: '5',
          children: [
            {
              label: 'Banner',
              value: '51',
              href: '/app/home/banner',
            },
            {
              label: '理论百家宴',
              value: '52',
              href: '/app/home/forum',
            },
            {
              label: '成长路线图',
              value: '53',
              href: '/app/home/grown',
            },
            {
              label: '专家演播室',
              value: '54',
              href: '/app/home/expertstudio',
            },
            {
              label: '名师工作室',
              value: '55',
              href: '/app/home/masterstudio',
            },
            {
              label: '名园风采录',
              value: '56',
              href: '/app/home/elegantrecord',
            },
            {
              label: '培训百宝箱',
              value: '57',
              href: '/app/home/train',
            }
          ]
        },
        {
          icon: 'inbox',
          label: '园所宝箱管理',
          value: '6',
          children: [
            {
              label: '改善办园条件百宝箱',
              value: '61',
              href: '/app/box/improveEnvBox',
            },
            {
              label: '打造组织文化百宝箱',
              value: '62',
              href: '/app/box/organizinCultural',
            },
            {
              label: '理顺管理机制百宝箱',
              value: '63',
              href: '/app/box/managementMechanism',
            },
            {
              label: '提升队伍素质百宝箱',
              value: '64',
              href: '/app/box/teamQuality',
            },
            {
              label: '开展园本教研百宝箱',
              value: '65',
              href: '/app/box/kindergartenBase',
            },
            {
              label: '引领家长提升百宝箱',
              value: '66',
              href: '/app/box/parentPromotion',
            },
            {
              label: '保育保健管理百宝箱',
              value: '67',
              href: '/app/box/healthManagement',
            },
            {
              label: '品牌形象包装百宝箱',
              value: '68',
              href: '/app/box/brandImage',
            },
            {
              label: '建构园本课程百宝箱',
              value: '69',
              href: '/app/box/buildCourse',
            },
            {
              label: '财务后勤管理',
              value: '611',
              href: '/app/box/financeAndLogister',
            },
            // {
            //   label: '疑难困惑百宝箱',
            //   value: '612',
            //   href: '/app/box/question',
            // },
          ]
        },
        {
          icon: 'desktop',
          label: '工作百宝箱管理',
          value: '7',
          children: [
            {
              label: '董事长工作百宝箱',
              value: '71',
              href: '/app/work/chairman',
            },
            {
              label: '园长工作百宝箱',
              value: '72',
              href: '/app/work/director',
            },
            {
              label: '后勤园长工作百宝箱',
              value: '73',
              href: '/app/work/logister_director',
            },
            {
              label: '业务园长工作百宝箱',
              value: '74',
              href: '/app/work/business_director',
            },
            {
              label: '年级组长工作百宝箱',
              value: '75',
              href: '/app/work/cg_leader',
            },
            {
              label: '教研组长工作百宝箱',
              value: '76',
              href: '/app/work/eg_leader',
            },
            {
              label: '主班教师工作百宝箱',
              value: '77',
              href: '/app/work/m_teacher',
            },
            {
              label: '配班教师工作百宝箱',
              value: '78',
              href: '/app/work/teacher',
            },
            {
              label: '保健医生工作百宝箱',
              value: '79',
              href: '/app/work/c_doctor',
            },
            {
              label: '保育员工作百宝箱',
              value: '710',
              href: '/app/work/nurse',
            },
            {
              label: '保安工作百宝箱',
              value: '711',
              href: '/app/work/security',
            },
            {
              label: '保管工作百宝箱',
              value: '712',
              href: '/app/work/security_man',
            },
            {
              label: '保洁工作百宝箱',
              value: '713',
              href: '/app/work/cleaner',
            },
            {
              label: '财务工作百宝箱',
              value: '714',
              href: '/app/work/finance',
            },
            {
              label: '采购工作百宝箱',
              value: '715',
              href: '/app/work/procure',
            },
            {
              label: '厨师工作百宝箱',
              value: '716',
              href: '/app/work/chief',
            },
            {
              label: '校车司机工作百宝箱',
              value: '717',
              href: '/app/work/driver',
            },
          ]
        },
        {
          icon: 'shopping-cart',
          label: '商城',
          value: '8',
          children: [
            {
              label: '商品库管理',
              value: '81',
              href: '/app/shoppingmall/goodsManagement',
            },
            {
              label: '首页管理',
              value: '82',
              href: '/app/shoppingmall/homeManagement',
            },
            {
              label: '商城文库',
              value: '83',
              href: '/app/shoppingmall/documents',
            },
            {
              label: '规格管理',
              value: '84',
              href: '/app/shoppingmall/spec',
            }
          ]
        },
        {
          icon: 'unordered-list',
          label: '订单管理',
          value: '9',
          children: [
            {
              label: '订单管理',
              value: '91',
              href: '/app/shoppingmall/orderManagement',
            },
            {
              label: '退款申请',
              value: '92',
              href: '/app/shoppingmall/chargeBackManagement',
            }
          ]
        },
        {
          icon: 'check-square',
          label: '账单管理',
          value: '10',
          children: [
            {
              label: '账单流水',
              value: '101',
              href: '/app/shoppingmall/orderCashFlow',
            },
          ]
        },
        {
          icon: 'cloud-download',
          label: '云盘管理',
          value: '11',
          children: [
            {
              label: '资源包管理',
              value: '111',
              href: '/app/shoppingmall/goodsManagement',
            }
          ]
        },
      ];
      yield put({ type: 'setMenus', payload: menus });
      if (payload) yield put({ type: 'setDefaultMenus', payload });
    },
  },
  reducers: {
    /**
     * 设置默认展开的菜单项
     * @param state
     * @param param1
     */
    setDefaultMenus(state, { payload }: any) {
      const { menus } = state;
      const { defaultOpenKey, defaultSelectedKey } = matchPathName(menus, payload);
      state.defaultOpenKeys = defaultOpenKey ? [defaultOpenKey] : [];
      state.defaultSelectedKeys = defaultSelectedKey ? [defaultSelectedKey] : [];
      return state;
    },
    setMenus(state, { payload }: any) {
      state.menus = payload;
      return state;
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        dispatch({
          type: 'fetchMenus',
          payload: pathname
        })
      });
    },
  }
} as Model

